package com.wctj.api.base.utils;

import org.apache.commons.lang3.StringUtils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
* @Description: 加密工具类
**/
public class EncryptUtil {

    public static final String MD5 = "MD5";

    public static final String SHA_1 = "SHA-1";

    private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };

    /**
    * @Description: md5加密
    * @Author: qy
    * @Date: 2022/3/17 21:59
    * @param text: 待加密字符串
    * @return: 加密后字符串
    **/
    public static String toMD5(String text) {
        //定义一个字节数组
        byte[] secretBytes;
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance(MD5);
            //对字符串进行加密,规定加密编码格式，统一为utf-8
            md.update(text.getBytes(StandardCharsets.UTF_8));
            //获得加密后的数据
            secretBytes = md.digest();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有md5这个算法！");
        }
        //将加密后的数据转换为16进制数字
        StringBuilder md5code = new StringBuilder(new BigInteger(1, secretBytes).toString(16)); // 16进制数字
        // 如果生成数字未满32位，需要前面补0
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code.append("0");
        }
        return md5code.toString();
    }


    /**
    * @Description: HmacSHA256
    * @Author: qy
    **/
    public static String hmacSHA256(String data, String key) throws Exception {
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
        sha256_HMAC.init(secret_key);
        byte[] array = sha256_HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100), 1, 3);
        }
        return sb.toString();
    }


    /**
    * @Description: SHA-1加密
    * @Author: qy
    * @Date: 2022/3/24 14:05
    **/
    public static String SHA1(String data){
        if (StringUtils.isBlank(data)) return null;
        try {
            MessageDigest sha1 = MessageDigest.getInstance(SHA_1);
            byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
            final byte[] digest = sha1.digest(bytes);
            final int l = digest.length;
            final char[] out = new char[l << 1];
            // two characters form the hex value.
            for (int i = 0, j = 0; i < l; i++) {
                out[j++] = DIGITS_LOWER[(0xF0 & digest[i]) >>> 4];
                out[j++] = DIGITS_LOWER[0x0F & digest[i]];
            }
            return new String(out);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有SHA-1这个算法！");
        }
    }

//    public static void main(String[] args) {
//        String text =  "{\"event\":\"verify_webhook\",\"client_key\":\"\",\"content\":{\"challenge\":12345}}";
//        System.out.println(EncryptUtil.SHA1(text));
//        System.out.println(DigestUtils.sha1Hex(text));
//    }


}
